package com.eobard.utils;

import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;

/**
 * @Author: Eobard Gu
 * @Date: 2025-08-06
 * @Description: 临时文件清理
 */
@Component
@Slf4j
public class TempDirCleaner implements DisposableBean {
    @Getter
    private static File tempDir;

    public static void setTempDir(File file) {
        tempDir = file;
    }

    @Override
    public void destroy() {
        if (ObjectUtil.isNotEmpty(tempDir) && tempDir.exists()) {
            log.info("shutting down the jvm, ready to clean the temp directory:{}", tempDir);
            FileUtils.deleteDirFileRecursively(tempDir);
        }
    }

}
